/* pad.cc
* This file is part of ncursesxx library
* Copyright (c) 2003,2004 by Stanislav Ievlev
*
* This file is covered by the GNU Library General Public License,
* which should be included with libncursesxx as the file COPYING.
*/
#include <stdexcept>

#include <ncurses/window.hh>
#include <ncurses/pad.hh>
#include <ncurses/assert.hh>

using namespace ncurses;

pad::pad():
		counter_(),
		pad_(0)
{
	pad_ = ::newpad(1, 1);
	if (!pad_)
		throw std::runtime_error("newpad failed");
	::keypad(pad_, TRUE); //initialize advanced keycodes for this window
}

pad::pad(const pad& other):
		counter_(other.counter_),
		pad_(other.pad_)
{}

pad::~pad()
{
	if (1 == *counter_.usage_) ::delwin(pad_);
}

void pad::swap(pad& other)
{
	std::swap(pad_, other.pad_);
	counter_.swap(other.counter_);
}

pad& pad::operator=(const pad& other)
{
	pad(other).swap(*this);
	return *this;
}

void pad::resize(unsigned int w, unsigned int h)
{
	NCURSES_ASSERT(::wresize(pad_, h, w));
}

unsigned int pad::width() const
{
	int x, y;
	getmaxyx(pad_, y, x);
	return x;
}

unsigned int pad::height() const
{
	int x, y;
	getmaxyx(pad_, y, x);
	return y;
}

void pad::draw(window& viewport, int x, int y)
{
	//ncurses assert here?
	NCURSES_ASSERT(::copywin(pad_, viewport.win(), y, x, 0, 0,
				 viewport.height() - 1, viewport.width() - 1, FALSE));
}
